<?php
namespace app\api\controller\approval;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\common\model\rebate\RebateList;
use app\common\model\rebate\Rebate as RebateModel;
use app\api\controller\Base;
use app\api\controller\Baseuser;
use app\common\model\revenue_ap\Raplist as RapListModel;
use app\common\model\revenue_ap\Rap as RapModel;
use app\common\model\assay\Assaylist;
use app\common\model\approval\Approvalrole;
use app\common\model\approval\Approvallog as ApprovalLog;
use app\api\controller\mytool\Sendmessage;
use app\common\model\company\Contract as Contractmodel;


//审批
class Approval extends Baseuser
{
	//初始化
    public function initialize(){
    	parent::initialize();
		// Rolepower::powerguard(101004);
	}

	/**
	 * 创建审批订单
	 * @return [type] [description]
	 */
	public function approval(){
		$idata = input('post.');
		if(empty($idata['State']) || empty($idata['Id'])||empty($idata['Step'])){
			return myerr('缺少参数，请重新尝试');
		}
		$olddata = ApprovalLog::get($idata['Id']);
		if(empty($olddata)){
			return myerr('没找到需要的审批订单');
		}
		$olddata = $olddata->toArray();
		//找到需要调整的规则信息
		$olddata['RulesPlan'] = json_decode($olddata['RulesPlan'],true);
		$lsdata = [];
		//需要审批的 下个节点的标识 
		$nextitemuser = [];
		$successflag = false;
		foreach ($olddata['RulesPlan']['Rules'] as $key => $value) {
			if($value['Step']==$idata['Step']){
				if(empty($value['list'][$idata['ListIndex']])){
					return myerr('没有找到需要审批环节信息');
				}
				$lsdata = $value['list'][$idata['ListIndex']];
				//判断当前节点除了此人的是否还有其他人未审批的
				$sb_step_wait = false;
				foreach ($value['list'] as $vk => $vv) {
					if((int)$vk==$idata['ListIndex']){continue;}
					if($vv['State']!==2){
						$sb_step_wait = true;//此节点还存在没有审核的
						break;
					}
				}
				//判断当前节点是否已经结束，如果结束了进行下一个节点的操作
				if(!$sb_step_wait){//当前节点已经结束
					//判断是否还存在下个节点
					if(!empty($olddata['RulesPlan']['Rules'][$key+1])){
						//判断下个循环量是否存在
						if($olddata['RulesPlan']['Rules'][$key+1]['Type']=='会签'){
							$nextitemuser = $olddata['RulesPlan']['Rules'][$key+1]['UserIds'];
						}else{
							$nextitemuser = $olddata['RulesPlan']['Rules'][$key+1]['list'][0]['UserId'];
						}
					}else{
						$successflag = true;
					}
				}else{//当前节点未结束
					//判断此节点是否为顺签
					if($olddata['RulesPlan']['Rules'][$key]['Type']=='顺签'){
						//判断下一位是否存在
						if(!empty($value['list'][(int)$idata['ListIndex']+1])){
							$nextitemuser = $value['list'][(int)$idata['ListIndex']+1]['UserId'];
						} 
					}
				}
				
			}
		}
		if(empty($lsdata)){
			return myerr('没有找到需要审批环节信息');
		}
		//判断是否为此人是否是需要审批人的id
		if((int)$lsdata['UserId']!==$this->user_id){
			return myerr('此环节无需您审批');
		}
		//检测此信息是否已经审批通过了
		if($lsdata['State']=='2'&&$idata['State']=='3'){
			return myerr('您已经审批通过此环节,无法驳回');
		}
		//审批状态
		if($idata['State']=='3'){
			//驳回审批
			ApprovalLog::re_rules_list_info($idata,$idata['Step'],$idata['ListIndex'],$olddata);
			//通知创建人
			Sendmessage::approval(2,$olddata);
		}elseif($idata['State']=='2'){
			//通过审核
			ApprovalLog::re_rules_list_info($idata,$idata['Step'],$idata['ListIndex'],$olddata);
			//判断需要通知下个审批人 
			if(!empty($nextitemuser)){
				//通知下个审批负责人
				Sendmessage::approval(1,$olddata,$nextitemuser);
			}
			//判断是否已经完成所有审批-》完成后需要走审批完成的流程
			if($successflag){
				//修改主表状态
				$modal = ApprovalLog::get($idata['Id']);
				$modal->State = ApprovalLog::STATEOK;
				$modal->save();
				//修改关联信息表
				switch ($olddata['Type']) {
					case 'assay':
						$sb = Assaylist::get($olddata['CorrelationId']);
						$sb->State = Assaylist::STATEAPPROVAL;
						$sb->save();
						break;
					case 'contract':
						$sb = Contractmodel::get($olddata['CorrelationId']);
						$sb->State = Contractmodel::STATEOK;
						$sb->save();
						break;
				}
				//通知创建人，审批已完成
				Sendmessage::approval(3,$olddata);
			}
			return myok('审批完成');
		}else{
			return moerr('审批状态不正确');
		}
		return myok();
	}


	
}
